package com.czxy.service;

import com.czxy.mapper.UserMapper;
import com.czxy.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

/**
 * @author 张青卯
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class UserService {

    private final UserMapper userMapper;

    @Autowired
    public UserService(UserMapper userMapper) {
        this.userMapper = userMapper;
    }

    /**
     * saveUser 注册方法 注册功能
     * @param user 接收对象往数据库中添加用户信息
     */
    public void saveUser(User user){
        User user1 = selectOne(user);
        if (user1==null){
            userMapper.insert(user);
        }
    }

    /**
     * selectOne 查询方法 短信功能
     * @param user 接收对象查找对应数据
     * @return user
     */
    public User selectOne(User user){
        Example example = new Example(User.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("mobile",user.getMobile());
        return userMapper.selectOneByExample(example);
    }

    /**
     * findUserByMobileAndPassword 查找是否有用户信息
     * @param mobile 手机号
     * @param password 密码
     * @return user对象
     */
    public User findUserByMobileAndPassword(String mobile,String password){
        Example example = new Example(User.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("mobile",mobile);
        criteria.andEqualTo("password",password);
        return userMapper.selectOneByExample(example);
    }
}
